package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysSetting;
import com.ruoyi.system.service.ISysSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 参数配置Controller
 *
 * @author siashan
 * @date 2024-02-22
 */
@RestController
@RequestMapping("/system/setting")
public class SysSettingController extends BaseController {

    @Autowired
    private ISysSettingService sysSettingService;

    /**
     * 查询参数配置列表
     *
     * @param sysSetting 参数
     * @return 列表
     */
    @PreAuthorize("@ss.hasPermi('system:setting:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysSetting sysSetting) {
        startPage();
        LambdaQueryWrapper<SysSetting> lqw = new LambdaQueryWrapper<SysSetting>();
        if (StringUtils.isNotBlank(sysSetting.getConfigName())) {
            lqw.like(SysSetting::getConfigName, sysSetting.getConfigName());
        }
        if (StringUtils.isNotBlank(sysSetting.getConfigKey())) {
            lqw.eq(SysSetting::getConfigKey, sysSetting.getConfigKey());
        }
        if (StringUtils.isNotBlank(sysSetting.getConfigValue())) {
            lqw.eq(SysSetting::getConfigValue, sysSetting.getConfigValue());
        }
        if (StringUtils.isNotBlank(sysSetting.getConfigType())) {
            lqw.eq(SysSetting::getConfigType, sysSetting.getConfigType());
        }
        List<SysSetting> list = sysSettingService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 获取参数配置详细信息
     *
     * @param configId id
     * @return 详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:setting:query')")
    @GetMapping(value = "/{configId}")
    public AjaxResult getInfo(@PathVariable("configId") Long configId) {
        return success(sysSettingService.getById(configId));
    }

    /**
     * 按configKey获取参数配置详细信息
     *
     * @param configKey key
     * @return 详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:setting:query')")
    @GetMapping(value = "/getInfoByKey/{configKey}")
    public AjaxResult getInfoByKey(@PathVariable("configKey") String configKey) {
        SysSetting setting = sysSettingService.getByConfigKey(configKey);
        if (null == setting){
            return AjaxResult.error("无此配置，请确认！");
        }
        return success(setting);
    }

    /**
     * 新增参数配置
     *
     * @param sysSetting 参数
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('system:setting:add')")
    @Log(title = "参数配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysSetting sysSetting) {
        return toAjax(sysSettingService.save(sysSetting) ? 1 : 0);
    }

    /**
     * 修改参数配置
     *
     * @param sysSetting 参数
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('system:setting:edit')")
    @Log(title = "参数配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysSetting sysSetting) {
        return toAjax(sysSettingService.updateById(sysSetting) ? 1 : 0);
    }

    /**
     * 删除参数配置
     *
     * @param configIds 主键id数组
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('system:setting:remove')")
    @Log(title = "参数配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configIds}")
    public AjaxResult remove(@PathVariable Long[] configIds) {
        return toAjax(sysSettingService.removeByIds(Arrays.asList(configIds)) ? 1 : 0);
    }
}
